<template>
  <div id="addPicturePage">
    <h2 style="margin-bottom: 16px">批量创建图片</h2>

    <!--    输入图片信息功能-->
    <a-form layout="vertical" :model="formData" @finish="handleSubmit">
      <a-form-item label="关键词" name="searchText">
        <a-input v-model:value="formData.searchText" placeholder="请输入搜索关键词" />
      </a-form-item>
      <a-form-item label="抓取数量" name="count">
        <a-input-number
          v-model:value="formData.count"
          placeholder="请输入抓取数量"
          style="min-width: 180px"
          :min="1"
          :max="30"
          allowClear
        />
      </a-form-item>
      <a-form-item label="设置名称前缀" name="namePrefix">
        <a-input v-model:value="formData.namePrefix" placeholder="请输入前缀名，会自动补充序号" />
      </a-form-item>
      <a-form-item label="分类" name="category">
        <a-auto-complete
            v-model:value="formData.category"
            :options="categoryOptions"
            placeholder="请输入分类"
            allowClear
        />
      </a-form-item>
      <a-form-item label="标签" name="tags">
        <a-select
            v-model:value="formData.tags"
            :options="tagOptions"
            mode="tags"
            placeholder="请选择或输入标签"
            :max-tag-count="3"
            allowClear
            @search="handleTagSearch"
            @change="handleTagChange"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%" :loading="loading"
          >开始搜索
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import {computed, onMounted, reactive, ref} from 'vue'
import { useRouter } from 'vue-router'
import {listPictureTagCategoryUsingGet, uploadPictureByBatchUsingPost} from '@/api/pictureController'
import { message } from 'ant-design-vue'

const router = useRouter()

const formData = reactive<API.PictureUploadByBatchRequest>({
  count: 10,
})
const loading = ref(false)

/**
 * 提交表单函数
 * @param values
 */
const handleSubmit = async (values: any) => {
  loading.value = true //按钮变为加载中
  // 后端接收结果，解析values（即表单内容）
  const res = await uploadPictureByBatchUsingPost({
    ...formData,
  })
  if (res.data.code === 0 && res.data.data) {
    message.success(`创建成功，共${res.data.data}条`)
    //跳转到主页
    router.push({
      path: '/',
    })
  } else {
    message.error('图片操作失败，' + res.data.message)
  }
  loading.value = false
}

/**
 * 分类和标签输入补充选项数据
 */
const categoryOptions = ref<string[]>([])
const tagOptions = ref<string[]>([])

// 获取标签和分类选项
const getTagCategoryOptions = async () => {
  const res = await listPictureTagCategoryUsingGet()
  if (res.data.code === 0 && res.data.data) {
    //转换成下拉选项组件接受的格式
    tagOptions.value = (res.data.data.tagList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => {
      return {
        value: data,
        label: data,
      }
    })
  } else {
    message.error('加载选项失败，' + res.data.message)
  }
}

onMounted(() => {
  getTagCategoryOptions()
})

/**
 * 处理标签限制
 */
// 是否已达到最大标签数
const isTagsFull = computed(() => formData.tags.length >= 3);

// 处理标签变化
const handleTagChange = (newTags) => {
  if (newTags.length > 3) {
    formData.tags = newTags.slice(0, 3);
    message.error('最多只能有三个标签');
  }
};

// 处理标签输入
const handleTagSearch = (inputValue, { prefixCls, setOpen }) => {
  if (isTagsFull.value) {
    message.error('已达到最大标签数量');
    setOpen(false);
    return;
  }
}

</script>
<style scoped>
#addPicturePage {
  max-width: 720px;
  margin: 0 auto;
}
</style>
